define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'registr_activity_user/index/ids/'+Config.activity_id + location.search,
                    add_url: 'registr_activity_user/add/id/'+Config.activity_id ,
                    edit_url: 'registr_activity_user/edit',
                    del_url: 'registr_activity_user/del',
                    multi_url: 'registr_activity_user/multi',
                    import_url: 'registr_activity_user/import',
                    table: 'registr_activity_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormTemplate: 'customformtpl',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:true,
                // commonSearch:false,
          
            
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '歷史登記列表_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['operate'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'registractivity.name', title: __('Registractivity.name'), operate: 'LIKE'},
                          {field: 'source', title: __('來源'), searchList: {"0":'掃碼登記',"1":'手動登記'}, formatter: Table.api.formatter.normal},
                       
                        // {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'gender', title: __('Gender'), searchList: {"1":'男',"2":'女'}, formatter: Table.api.formatter.normal},
                        {field: 'birthday', title: __('Birthday'), operate: false, formatter: Table.api.formatter.normal},
                        {field: 'id_card', title: __('Id_card'), operate: false, formatter: Table.api.formatter.normal},
                        {field: 'return_permit', title: __('Return_permit'), operate: false, formatter: Table.api.formatter.normal},
                        {field: 'hk_address', title: __('Hk_address'), operate: false, formatter: Table.api.formatter.normal},
                        {field: 'nd_address', title: __('Nd_address'), operate: false, formatter: Table.api.formatter.normal},
                        {field: 'hk_mobile', title: __('Hk_mobile'), operate: false, formatter: Table.api.formatter.normal},
                        {field: 'nd_mobile', title: __('Nd_mobile'), operate: false, formatter: Table.api.formatter.normal},
                        {field: 'email', title: __('Email'), operate: false, formatter: Table.api.formatter.normal},
                        {field: 'fm_mobile', title: __('Fm_mobile'), operate: false, formatter: Table.api.formatter.normal},
                        {field: 'native_place', title: __('Native_place'), operate: false, formatter: Table.api.formatter.normal},
                        {field: 'education', title: __('Education'), searchList: {"1":__('education 1'),"2":__('education 2'),"3":__('education 3'),"4":__('education 4')}, formatter: Table.api.formatter.normal},
                        {field: 'cognitive_approach', title: __('Cognitive_approach'), searchList: {"1":__('cognitive_approach 1'),"2":__('cognitive_approach 2'),"3":__('cognitive_approach 3'),"4":__('cognitive_approach 4'),"5":__('cognitive_approach 5'),"6":__('cognitive_approach 6'),"7":__('cognitive_approach 7'),"8":__('cognitive_approach 8'),"9":__('cognitive_approach 9'),"10":__('cognitive_approach 10'),"11":__('cognitive_approach 11')}, formatter: Table.api.formatter.normal},
                        {field: 'is_voter', title: __('Is_voter'), searchList: {"1":'是',"2":'否'}, formatter: Table.api.formatter.normal},
                        {field: 'trade_union', title: __('Trade_union'), operate: false, formatter: Table.api.formatter.normal},
                        {field: 'consultationcategory.name', title: __('咨詢類別'), operate: false},
                        {field: 'consultation_content', title: __('Consultation_content'),width:'30%', operate: false},
                        {field: 'remark', title: __('Remark'),width:'30%', operate: false},
                        {field: 'createtime', title: __('Createtime'),sortable: true, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:function(value,row,index){
                                    this.buttons = [
                                        {
                                            name: 'edit',
                                            text: '詳情',
                                            classname: 'btn btn-xs btn-success btn-magic btn-editone',
                                            url: 'registr_activity_user/edit',
                                            // confirm: '确认禁用嗎？',
                                            visible: function (row) {
                                                if ( Config.registr_activity_user_edit ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },
                                {
                                    name: 'del',
                                    text: '删除',
                                    title: '删除',
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    icon: '',
                                    url: 'registr_activity_user/del',
                                    confirm: '确认删除吗?',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if ( Config.registr_activity_user_del ) {
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                }
                                    ]
                                return Table.api.formatter.operate.call(this,value,row,index);
                            }}
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

           user_list: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'registr_activity_user/user_list/ids/'+ Config.ids + location.search,
                    add_url: 'staff/add',
                    edit_url: 'staff/edit',
                    del_url: 'staff/del',
                    multi_url: 'staff/multi',
                    import_url: 'staff/import',
                    table: 'staff',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: false,
                search:false,
                showColumns:false,
                showExport:true,
                commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '登記記錄_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['avatar'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                   

                  

                        {field: 'activity_name', title: __('活動名稱'), operate: 'LIKE'},
                        {field: 'createtime', title: __('來訪時間'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                     
                        {field: 'name', title: __('姓名'), operate: 'LIKE'},
                        {field: 'id_card', title: __('香港身份證號碼'), operate: 'LIKE'},
                        {field: 'hk_mobile', title: __('香港電話'), operate: 'LIKE'},
                        {field: 'hk_address', title: __('香港地址'), operate: 'LIKE'},
                        {field: 'nd_mobile', title: __('内地電話'), operate: 'LIKE'},
                        {field: 'nd_address', title: __('内地地址'), operate: 'LIKE'},
                        {field: 'consultation_content', title: __('諮詢内容')},

                         
                    ]
                ]
            });
              table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'registr_activity_user/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'registr_activity_user/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'registr_activity_user/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});